﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;

namespace SignalRHost.Hubs
{
    public class ChatHub : Hub
    {
        private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();

        public void Hello()
        {
            Clients.All.hello();
        }

        public void Send(string name, string message)
        {
            // Call the broadcastMessage method to update clients.

            Console.WriteLine("Host Send()");

            Clients.All.broadcastMessage(name, message);
        }

        public override System.Threading.Tasks.Task OnConnected()
        {
            //string name = Context.User.Identity.Name;

            //_connections.Add(name, Context.ConnectionId);

            //Clients.All.broadcastMessage(name, name + " (" + Context.ConnectionId + ")have join the chatroom");

            Console.WriteLine("Host OnConnected()");

            Clients.All.broadcastMessage("System", "Someone have joined the chatroom.");

            return base.OnConnected();
        }

        public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
        {
            //string name = Context.User.Identity.Name;

            //_connections.Remove(name, Context.ConnectionId);

            //Clients.All.broadcastMessage(name, name + " have left the chatroom");

            Console.WriteLine("Host OnDisconnected()");

            Clients.All.broadcastMessage("System", "Someone have left the chatroom.");

            return base.OnDisconnected(stopCalled);
        }

        public override System.Threading.Tasks.Task OnReconnected()
        {
            //string name = Context.User.Identity.Name;

            //if (!_connections.GetConnections(name).Contains(Context.ConnectionId))
            //{
            //    _connections.Add(name, Context.ConnectionId);

            //    Clients.All.broadcastMessage(name, name + " (" + Context.ConnectionId + ") have join the chatroom");
            //}

            Console.WriteLine("Host OnReconnected()");

            Clients.All.broadcastMessage("System", "Someone have joined the chatroom.");

            return base.OnReconnected();
        }

        //public System.Threading.Tasks.Task JoinRoom(string roomName)
        //{
        //    return Groups.Add(Context.ConnectionId, roomName);
        //}

        ////public async Task JoinRoom(string roomName)
        ////{
        ////    await Groups.Add(Context.ConnectionId, roomName);
        ////    Clients.Group(roomName).addChatMessage(Context.User.Identity.Name + " joined.");
        ////}

        //public System.Threading.Tasks.Task LeaveRoom(string roomName)
        //{
        //    return Groups.Remove(Context.ConnectionId, roomName);
        //}
    }
}
